package com.hontech.bread.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.fragment.app.Fragment
import com.hontech.bread.R
import com.hontech.bread.device.breadDownMotorBottom
import com.hontech.bread.device.breadDownMotorTop
import com.hontech.bread.device.deliveryDelivery
import com.hontech.bread.device.deliveryLeft
import com.hontech.bread.device.deliveryRight
import com.hontech.bread.device.heatOff
import com.hontech.bread.device.heatOn
import com.hontech.bread.device.leftPushPush
import com.hontech.bread.device.leftPushReset
import com.hontech.bread.device.leftRotationClose
import com.hontech.bread.device.leftRotationRow
import com.hontech.bread.device.middlePushBack
import com.hontech.bread.device.middlePushFront
import com.hontech.bread.device.pickLedOff
import com.hontech.bread.device.pickLedOn
import com.hontech.bread.device.reset
import com.hontech.bread.device.rightPushPush
import com.hontech.bread.device.rightPushReset
import com.hontech.bread.device.rightRotationClose
import com.hontech.bread.device.rightRotationRow
import com.hontech.bread.ui.dialog.runProg
import com.hontech.bread.ui.view.LabelInput
import com.hontech.bread.ui.view.LabelSpinner
import com.hontech.bread.utils.onClick
import com.hontech.bread.utils.stringById

class MotorTestFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_motor_test, null)
        initUi(view)
        return view
    }

    private fun initUi(view: View) {
        breadDown(view)
        deliveryMotor(view)
        leftRotation(view)
        rightRotation(view)
        leftPush(view)
        rightPush(view)
        middlePush(view)
        pickLed(view)
        heat(view)
        delivery(view)
    }
}

private fun delivery(view: View) {
    val spLeftRows = view.findViewById<LabelSpinner>(R.id.id_motor_left_delivery_sp_row)
    val spRightRows = view.findViewById<LabelSpinner>(R.id.id_motor_right_delivery_sp_row)

    val btnReset = view.findViewById<Button>(R.id.id_motor_reset)

    val btnLeft = view.findViewById<Button>(R.id.id_motor_left_delivery)
    val btnRight = view.findViewById<Button>(R.id.id_motor_right_delivery)

    val liLeftTime = view.findViewById<LabelInput>(R.id.id_motor_delivery_heat_left)
    val liRightTime = view.findViewById<LabelInput>(R.id.id_motor_delivery_heat_right)

    btnReset.onClick {
        runProg(
            stringById(R.string.t1733460848_329),
            stringById(R.string.t1733460848_330)
        ) { dialog ->
            reset { dialog.setMsg(it) }
        }
    }

    btnLeft.onClick {
        runProg(
            stringById(R.string.t1733460848_331),
            "${stringById(R.string.t1733460848_332)}:${spLeftRows.selectText()}"
        ) { dialog ->
            deliveryLeft(spLeftRows.selectText().toInt(), liLeftTime.textInt().toLong() * 1000) {
                dialog.setMsg(it)
            }
        }
    }
    btnRight.onClick {
        runProg(
            stringById(R.string.t1733460848_333),
            "${stringById(R.string.t1733460848_334)}:${spRightRows.selectText()}"
        ) { dialog ->
            deliveryRight(spRightRows.selectText().toInt(), liRightTime.textInt().toLong() * 1000) {
                dialog.setMsg(it)
            }
        }
    }
}


private fun heat(view: View) {
    val btnOn = view.findViewById<Button>(R.id.id_motor_heat_on)
    val btnOff = view.findViewById<Button>(R.id.id_motor_heat_off)

    btnOn.onClick {
        runProg(
            "${stringById(R.string.t1733460848_335)}",
            "${stringById(R.string.t1733460848_336)}"
        ) {
            heatOn()
        }
    }

    btnOff.onClick {
        runProg(
            "${stringById(R.string.t1733460848_337)}",
            "${stringById(R.string.t1733460848_338)}"
        ) {
            heatOff()
        }
    }
}

private fun pickLed(view: View) {
    val btnOpen = view.findViewById<Button>(R.id.id_motor_led_on)
    val btnClose = view.findViewById<Button>(R.id.id_motor_led_off)

    btnOpen.onClick {
        runProg(
            "${stringById(R.string.t1733460848_339)}",
            "${stringById(R.string.t1733460848_340)}"
        ) {
            pickLedOn()
        }
    }

    btnClose.onClick {
        runProg(
            "${stringById(R.string.t1733460848_341)}",
            "${stringById(R.string.t1733460848_342)}"
        ) {
            pickLedOff()
        }
    }
}

private fun middlePush(view: View) {
    val front = view.findViewById<Button>(R.id.id_motor_middle_push_front)
    val back = view.findViewById<Button>(R.id.id_motor_middle_push_back)

    front.onClick {
        runProg(
            "${stringById(R.string.t1733460848_343)}",
            "${stringById(R.string.t1733460848_344)}"
        ) {
            middlePushFront()
        }
    }
    back.onClick {
        runProg(
            "${stringById(R.string.t1733460848_345)}",
            "${stringById(R.string.t1733460848_346)}"
        ) {
            middlePushBack()
        }
    }
}

private fun rightPush(view: View) {
    val reset = view.findViewById<Button>(R.id.id_motor_right_push_back)
    val push = view.findViewById<Button>(R.id.id_motor_right_push_front)

    reset.onClick {
        runProg(
            "${stringById(R.string.t1733460848_347)}",
            "${stringById(R.string.t1733460848_348)}"
        ) {
            rightPushReset()
        }
    }
    push.onClick {
        runProg(
            "${stringById(R.string.t1733460848_349)}",
            "${stringById(R.string.t1733460848_350)}"
        ) {
            rightPushPush()
        }
    }
}

private fun leftPush(view: View) {
    val reset = view.findViewById<Button>(R.id.id_motor_btn_left_push_back)
    val push = view.findViewById<Button>(R.id.id_motor_btn_left_push_front)

    reset.onClick {
        runProg(
            "${stringById(R.string.t1733460848_351)}",
            "${stringById(R.string.t1733460848_352)}"
        ) {
            leftPushReset()
        }
    }
    push.onClick {
        runProg(
            "${stringById(R.string.t1733460848_353)}",
            "${stringById(R.string.t1733460848_354)}"
        ) {
            leftPushPush()
        }
    }
}

private fun rightRotation(view: View) {
    val rightSp = view.findViewById<LabelSpinner>(R.id.id_motor_right_sp_row)
    val rightBtn = view.findViewById<Button>(R.id.id_motor_right_rotation_exec)

    rightBtn.onClick {
        runProg(
            "${stringById(R.string.t1733460848_355)}",
            "${stringById(R.string.t1733460848_356)}:${rightSp.selectText()}${stringById(R.string.t1733460848_357)}"
        ) {
            try {
                rightRotationRow(rightSp.selectText().toInt())
            } finally {
                rightRotationClose()
            }
        }
    }
}

private fun leftRotation(view: View) {
    val leftSp = view.findViewById<LabelSpinner>(R.id.id_motor_left_sp_row)
    val leftBtn = view.findViewById<Button>(R.id.id_motor_left_rotation_exec)

    leftBtn.onClick {
        runProg(
            "${stringById(R.string.t1733460848_358)}",
            "${stringById(R.string.t1733460848_359)}:${leftSp.selectText()}${stringById(R.string.t1733460848_360)}"
        ) {
            try {
                leftRotationRow(leftSp.selectText().toInt())
            } finally {
                leftRotationClose()
            }
        }
    }
}

private fun breadDown(view: View) {

    val up = view.findViewById<Button>(R.id.id_motor_bread_up)
    val down = view.findViewById<Button>(R.id.id_motor_bread_down)

    up.onClick {
        runProg(
            "${stringById(R.string.t1733460848_361)}",
            "${stringById(R.string.t1733460848_362)}"
        ) {
            breadDownMotorTop()
        }
    }

    down.onClick {
        runProg(
            "${stringById(R.string.t1733460848_363)}",
            "${stringById(R.string.t1733460848_364)}"
        ) {
            breadDownMotorBottom()
        }
    }
}

private fun deliveryMotor(view: View) {
    val left = view.findViewById<Button>(R.id.id_motor_delivery_left)
    val right = view.findViewById<Button>(R.id.id_motor_delivery_right)
    val delivery = view.findViewById<Button>(R.id.id_motor_delivery_delivery)

    left.onClick {
        runProg(
            "${stringById(R.string.t1733460848_365)}",
            "${stringById(R.string.t1733460848_366)}"
        ) {
            deliveryLeft()
        }
    }
    right.onClick {
        runProg(
            "${stringById(R.string.t1733460848_367)}",
            "${stringById(R.string.t1733460848_368)}"
        ) {
            deliveryRight()
        }
    }
    delivery.onClick {
        runProg(
            "${stringById(R.string.t1733460848_369)}",
            "${stringById(R.string.t1733460848_370)}"
        ) {
            deliveryDelivery()
        }
    }
}

